<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键字" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="企业名称/法定代表人/所属行业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营状态" prop="openState">
        <el-select v-model="queryParams.openState" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.jingying_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属地区" prop="province">
        <el-select v-model="queryParams.province" placeholder="请选择所属地区" clearable>
          <el-option
            v-for="area in options"
            :key="area.value"
            :label="area.label"
            :value="area.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="录入时间">
        <el-date-picker
          v-model="daterangeInputTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['team:customer:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['team:customer:edit']"
        >修改
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange" border="1">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="企业名称" align="center" prop="customerName"/>
      <el-table-column label="法定代表人" align="center" prop="legalLeader"/>
      <el-table-column label="成立时间" align="center" prop="registerDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="经营状态" align="center" prop="openState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jingying_status" :value="scope.row.openState"/>
        </template>
      </el-table-column>
      <el-table-column label="所属省份" align="center" prop="province"/>
      <el-table-column label="所属行业" align="center" prop="industry"/>
      <el-table-column label="录入时间" align="center" prop="inputTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inputTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="录入人" align="center" prop="inputUserName"/>
      <el-table-column label="营销人" align="center" prop="inputUserName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['team:customer:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="openMessage(scope.row)"
          >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户信息对话框 -->

    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业名称:" prop="customerName" label-width="100px">
              <el-input v-model="form.customerName" placeholder="请输入企业名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法定代表人:" prop="legalLeader" label-width="100px">
              <el-input v-model="form.legalLeader" placeholder="请输入法定代表人"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="成立时间:" prop="registerDate" label-width="100px">
              <el-date-picker clearable
                              v-model="form.registerDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择成立时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经营状态:" prop="openState" label-width="100px">
              <el-select v-model="form.openState">
                <el-option
                  v-for="dict in dict.type.jingying_status"
                  :value="dict.value"
                  :label="dict.label"
                >{{ dict.label }}
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="所属省份:" prop="province" label-width="100px">
              <el-select v-model="form.province" placeholder="请选择所属地区">
                <el-option
                  v-for="area in options"
                  :key="area.value"
                  :label="area.label"
                  :value="area.label">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属行业:" prop="industry" label-width="100px">
              <el-input v-model="form.industry" placeholder="请输入所属行业"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="注册资本:" prop="regCapital">
          <el-input v-model="form.regCapital" placeholder="请输入注册资本" label-width="100px"/>
        </el-form-item>
        <el-form-item label="注册地址:" prop="regAddr">
          <el-input type="textarea" v-model="form.regAddr" placeholder="请输入注册地址" label-width="100px"/>
        </el-form-item>
        <el-form-item label="经营范围:" prop="scope">
          <el-input type="textarea" v-model="form.scope" placeholder="请输入经营范围" label-width="100px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
<!--    客户信息详情列表-->
    <el-dialog :visible.sync="openMe" width="900px" append-to-body>
      <el-descriptions title="客户信息详情列表" :column="3" border ref="one" :model="one">
        <el-descriptions-item label="企业名称" label-class-name="my-label" content-class-name="my-content"
                              >{{one.customerName}}</el-descriptions-item>
        <el-descriptions-item label="法定代表人" >{{one.legalLeader}}</el-descriptions-item>
        <el-descriptions-item label="成立时间">{{one.registerDate}}</el-descriptions-item>
        <el-descriptions-item label="经营状态">{{one.openState===0?"开业":open.openState===1?"注销":"破产"}}</el-descriptions-item>
        <el-descriptions-item label="所属省份">{{one.province}}</el-descriptions-item>
        <el-descriptions-item label="注册资金">{{one.regCapital}}</el-descriptions-item>
        <el-descriptions-item label="所属行业">{{one.industry}}</el-descriptions-item>
        <el-descriptions-item label="注册地址">{{one.regAddr}}</el-descriptions-item>
        <el-descriptions-item label="录入时间">{{one.inputTime}}</el-descriptions-item>
        <el-descriptions-item label="录入人">{{one.inputUserName}}</el-descriptions-item>
        <el-descriptions-item label="营销人">{{one.inputUserName}}</el-descriptions-item>
        <el-descriptions-item label="经营范围">{{one.scope}}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import {listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer} from "@/api/team/customer";
import {provinceAndCityData, CodeToText, TextToCode} from 'element-china-area-data'

export default {
  name: "Customer",
  dicts: ['jingying_status'],
  data() {
    return {
      options: provinceAndCityData,
      // 遮罩层
      loading: true,
      openMe: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      daterangeInputTime: [],
      // 总条数
      total: 0,
      // 客户信息表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        legalLeader: null,
        registerDate: null,
        openState: null,
        province: null,
        industry: null,
        inputTime: null,
        inputUser: null
      },
      // 表单参数
      form: {},
      one: {},
      // 表单校验
      rules: {
        customerName: [
          {required: true, trigger: "blur", message: "请输入企业名称"},
          {max: 100, message: '企业名称不能超过100个字', trigger: "blur"}
        ],
        legalLeader: [
          {required: true, trigger: "blur", message: "请输入法定代表人"},
          {max: 30, message: '法定代表人不能超过30个字', trigger: "blur"}
        ],
        registerDate: [
          {required: true, trigger: "blur", message: "请选择日期,格式为xxxx年xx月xx日"},
        ],
        openState: [
          {required: true, trigger: "blur", message: "请选择经营状态"},
        ],
        province: [
          {required: true, trigger: "blur", message: "请选择省份"},
        ],
        industry: [
          {required: true, trigger: "blur", message: "请输入所属行业"},
          {max: 30, message: '所属行业不能超过30个字', trigger: "blur"}
        ],
        regAddr: [
          {required: true, trigger: "blur", message: "请输入注册地址"},
          {max: 500, message: '注册地址不能超过30个字', trigger: "blur"}
        ],
        scope: [
          {required: true, trigger: "blur", message: "请输入经营范围"},
          {max: 500, message: '经营范围不能超过500个字', trigger: "blur"}
        ],
        regCapital: [
          {required: true, message: "请输入注册资金", trigger: "blur"},
          {
            pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
            message: '请输入正确格式,可保留两位小数'
          }
        ],
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeInputTime && '' != this.daterangeInputTime) {
        this.queryParams.params["beginInputTime"] = this.daterangeInputTime[0];
        this.queryParams.params["endInputTime"] = this.daterangeInputTime[1];
      }
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleChange(value) {
      console.log(value);
    },
    openMessage(row) {
      this.openMe = true;
      const id = row.id
      getCustomer(id).then(res => {
        this.one = res.data
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerName: null,
        legalLeader: null,
        registerDate: null,
        openState: null,
        province: null,
        regCapital: null,
        industry: null,
        scope: null,
        regAddr: null,
        inputTime: null,
        inputUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeInputTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户信息编号为"' + ids + '"的数据项？').then(function () {
        return delCustomer(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('team/customer/export', {
        ...this.queryParams
      }, `customer_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
